home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / mach / sun4c.md / pack.c < prev    next >
C/C++ Source or Header  |  1989-08-17  |  8KB  |  389 lines

  1. #ifdef sccsid
  2. static char     sccsid[] = "@(#)pack.c 1.7 88/03/03 SMI";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988 by Sun Microsystems, Inc.
  7.  */
  8.  
  9. /* Pack procedures for Sparc FPU simulator. */
  10.  
  11. /*
  12.  * This whole file refuses to lint.
  13.  */
  14. #ifndef lint
  15. #include <sun4/fpu/fpu_simulator.h>
  16. #include <sun4/fpu/globals.h>
  17.  
  18. PRIVATE int
  19. overflow_to_infinity(sign)
  20.     int             sign;
  21.  
  22. /* Returns 1 if overflow should go to infinity, 0 if to max finite. */
  23.  
  24. {
  25.     int             inf;
  26.  
  27.     switch (fp_direction) {
  28.     case fp_nearest:
  29.         inf = 1;
  30.         break;
  31.     case fp_tozero:
  32.         inf = 0;
  33.         break;
  34.     case fp_positive:
  35.         inf = !sign;
  36.         break;
  37.     case fp_negative:
  38.         inf = sign;
  39.         break;
  40.     }
  41.     return (inf);
  42. }
  43.  
  44. PRIVATE void
  45. round(pu)
  46.     unpacked       *pu;
  47.  
  48. /* Round according to current rounding mode.     */
  49.  
  50. {
  51.     int             increment;    /* boolean to indicate round up */
  52.  
  53.     if (pu->significand[2] == 0)
  54.         return;
  55.     fpu_set_exception(fp_inexact);
  56.     switch (fp_direction) {
  57.     case fp_nearest:
  58.         increment = pu->significand[2] >= 0x80000000;
  59.         break;
  60.     case fp_tozero:
  61.         increment = 0;
  62.         break;
  63.     case fp_positive:
  64.         increment = (pu->sign == 0) & (pu->significand[2] != 0);
  65.         break;
  66.     case fp_negative:
  67.         increment = (pu->sign != 0) & (pu->significand[2] != 0);
  68.         break;
  69.     }
  70.     if (increment) {
  71.         pu->significand[1]++;
  72.         if (pu->significand[1] == 0) {
  73.             pu->significand[0]++;
  74.             if (pu->significand[0] == 0) {    /* rounding carried out */
  75.                 pu->exponent++;
  76.                 pu->significand[0] = 0x80000000;
  77.             }
  78.         }
  79.     }
  80.     if ((fp_direction == fp_nearest) && (pu->significand[2] == 0x80000000)) {    /* ambiguous case */
  81.         pu->significand[1] &= 0xfffffffe;    /* force round to even */
  82.     }
  83. }
  84.  
  85. PRIVATE void
  86. packinteger(pu, px)
  87.     unpacked       *pu;    /* unpacked result */
  88.     int            *px;    /* packed integer */
  89. {
  90.     switch (pu->fpclass) {
  91.     case fp_zero:
  92.         *px = 0;
  93.         break;
  94.     case fp_normal:
  95.         if (pu->exponent >= 32)
  96.             goto overflow;
  97.         fpu_rightshift(pu, 63 - pu->exponent);
  98.         round(pu);
  99.         if (pu->significand[0] != 0)
  100.             goto overflow;
  101.         if (pu->significand[1] >= 0x80000000)
  102.             if ((pu->sign == 0) || (pu->significand[1] > 0x80000000))
  103.                 goto overflow;
  104.         *px = pu->significand[1];
  105.         if (pu->sign)
  106.             *px = -*px;
  107.         break;
  108.     case fp_infinity:
  109.     case fp_quiet:
  110.     case fp_signaling:
  111. overflow:
  112.         if (pu->sign)
  113.             *px = 0x80000000;
  114.         else
  115.             *px = 0x7fffffff;
  116.         _fp_current_exceptions &= ~(1 << (int) fp_inexact);
  117.         fpu_set_exception(fp_invalid);
  118.         break;
  119.     }
  120. }
  121.  
  122. PRIVATE void
  123. packsingle(pu, px)
  124.     unpacked       *pu;    /* unpacked result */
  125.     single_type    *px;    /* packed single */
  126. {
  127.     px->sign = pu->sign;
  128.     switch (pu->fpclass) {
  129.     case fp_zero:
  130.         px->exponent = 0;
  131.         px->significand = 0;
  132.         break;
  133.     case fp_infinity:
  134. infinity:
  135.         px->exponent = 0xff;
  136.         px->significand = 0;
  137.         break;
  138.     case fp_quiet:
  139.     case fp_signaling:
  140.         px->exponent = 0xff;
  141.         px->significand = 0x400000 | (0x3fffff & (pu->significand[0] >> 8));
  142.         break;
  143.     case fp_normal:
  144.         fpu_rightshift(pu, 40);
  145.         pu->exponent += SINGLE_BIAS;
  146.         if (pu->exponent <= 0) {
  147.             px->exponent = 0;
  148.             fpu_rightshift(pu, 1 - pu->exponent);
  149.             round(pu);
  150.             if (pu->significand[1] == 0x800000) {    /* rounded back up to
  151.                                  * normal */
  152.                 px->exponent = 1;
  153.                 px->significand = 0;
  154.                 return;
  155.             }
  156.             if (_fp_current_exceptions & (1 << fp_inexact))
  157.                 fpu_set_exception(fp_underflow);
  158.             px->significand = 0x7fffff & pu->significand[1];
  159.             return;
  160.         }
  161.         round(pu);
  162.         if (pu->significand[1] == 0x1000000) {    /* rounding overflow */
  163.             pu->significand[1] = 0x800000;
  164.             pu->exponent += 1;
  165.         }
  166.         if (pu->exponent >= 0xff) {
  167.             fpu_set_exception(fp_overflow);
  168.             fpu_set_exception(fp_inexact);
  169.             if (overflow_to_infinity(pu->sign))
  170.                 goto infinity;
  171.             px->exponent = 0xfe;
  172.             px->significand = 0x7fffff;
  173.             return;
  174.         }
  175.         px->exponent = pu->exponent;
  176.         px->significand = 0x7fffff & pu->significand[1];
  177.     }
  178. }
  179.  
  180. PRIVATE void
  181. packdouble(pu, px, py)
  182.     unpacked       *pu;    /* unpacked result */
  183.     double_type    *px;    /* packed double */
  184.     unsigned       *py;
  185. {
  186.     px->sign = pu->sign;
  187.     switch (pu->fpclass) {
  188.     case fp_zero:
  189.         px->exponent = 0;
  190.         px->significand = 0;
  191.         *py = 0;
  192.         break;
  193.     case fp_infinity:
  194. infinity:
  195.         px->exponent = 0x7ff;
  196.         px->significand = 0;
  197.         *py = 0;
  198.         break;
  199.     case fp_quiet:
  200.     case fp_signaling:
  201.         px->exponent = 0x7ff;
  202.         fpu_rightshift(pu, 11);
  203.         px->significand = 0x80000 | (0x7ffff & pu->significand[0]);
  204.         *py = pu->significand[1];
  205.         break;
  206.     case fp_normal:
  207.         fpu_rightshift(pu, 11);
  208.         pu->exponent += DOUBLE_BIAS;
  209.         if (pu->exponent <= 0) {    /* underflow */
  210.             px->exponent = 0;
  211.             fpu_rightshift(pu, 1 - pu->exponent);
  212.             round(pu);
  213.             if (pu->significand[0] == 0x100000) {    /* rounded back up to
  214.                                  * normal */
  215.                 px->exponent = 1;
  216.                 px->significand = 0;
  217.                 *py = 0;
  218.                 return;
  219.             }
  220.             if (_fp_current_exceptions & (1 << fp_inexact))
  221.                 fpu_set_exception(fp_underflow);
  222.             px->exponent = 0;
  223.             px->significand = 0xfffff & pu->significand[0];
  224.             *py = pu->significand[1];
  225.             return;
  226.         }
  227.         round(pu);
  228.         if (pu->significand[0] == 0x200000) {    /* rounding overflow */
  229.             pu->significand[0] = 0x100000;
  230.             pu->exponent += 1;
  231.         }
  232.         if (pu->exponent >= 0x7ff) {    /* overflow */
  233.             fpu_set_exception(fp_overflow);
  234.             fpu_set_exception(fp_inexact);
  235.             if (overflow_to_infinity(pu->sign))
  236.                 goto infinity;
  237.             px->exponent = 0x7fe;
  238.             px->significand = 0xfffff;
  239.             *py = 0xffffffff;
  240.             return;
  241.         }
  242.         px->exponent = pu->exponent;
  243.         px->significand = 0xfffff & pu->significand[0];
  244.         *py = pu->significand[1];
  245.         break;
  246.     }
  247. }
  248.  
  249. PRIVATE void
  250. packextended(pu, px, py, pz)
  251.     unpacked       *pu;    /* unpacked result */
  252.     extended_type  *px;    /* packed extended */
  253.     unsigned       *py, *pz;
  254. {
  255.     px->sign = pu->sign;
  256.     px->unused = 0;
  257.     switch (pu->fpclass) {
  258.     case fp_zero:
  259.         px->exponent = 0;
  260.         *pz = 0;
  261.         *py = 0;
  262.         break;
  263.     case fp_infinity:
  264. infinity:
  265.         px->exponent = 0x7fff;
  266.         *pz = 0;
  267.         *py = 0;
  268.         break;
  269.     case fp_quiet:
  270.     case fp_signaling:
  271.         px->exponent = 0x7fff;
  272.         *py = 0x40000000 | pu->significand[0];    /* Insure quiet nan. */
  273.         *pz = pu->significand[1];
  274.         break;
  275.     case fp_normal:
  276.         pu->exponent += EXTENDED_BIAS;
  277.         if (pu->exponent < 0) {    /* underflow */
  278.             fpu_rightshift(pu, -pu->exponent);
  279.             round(pu);
  280.             if (pu->significand[0] < 0x80000000) {    /* not rounded back up
  281.                                  * to normal */
  282.                 if (_fp_current_exceptions & (1 << fp_inexact))
  283.                     fpu_set_exception(fp_underflow);
  284.             }
  285.             px->exponent = 0;
  286.             *py = pu->significand[0];
  287.             *pz = pu->significand[1];
  288.             return;
  289.         }
  290.         round(pu);    /* rounding overflow handled in round() */
  291.         if (pu->exponent >= 0x7fff) {    /* overflow */
  292.             fpu_set_exception(fp_overflow);
  293.             fpu_set_exception(fp_inexact);
  294.             if (overflow_to_infinity(pu->sign))
  295.                 goto infinity;
  296.             px->exponent = 0x7ffe;    /* overflow to max norm */
  297.             *py = 0xffffffff;
  298.             *pz = 0xffffffff;
  299.             return;
  300.         }
  301.         px->exponent = pu->exponent;
  302.         *py = pu->significand[0];
  303.         *pz = pu->significand[1];
  304.         break;
  305.     }
  306. }
  307.  
  308. void
  309. _fp_pack(pu, n, type)
  310.     unpacked       *pu;    /* unpacked operand */
  311.     unsigned        n;    /* register where datum starts */
  312.     enum fp_op_type type;    /* type of datum */
  313.  
  314. {
  315.     switch (type) {
  316.     case fp_op_integer:
  317.         {
  318.             int             x;
  319.  
  320.             packinteger(pu, &x);
  321.             _fp_current_write_freg(&x, n);
  322.             break;
  323.         }
  324.     case fp_op_single:
  325.         {
  326.             single_type     x;
  327.  
  328.             packsingle(pu, &x);
  329.             _fp_current_write_freg(&x, n);
  330.             break;
  331.         }
  332.     case fp_op_double:
  333.         {
  334.             double_type     x;
  335.             unsigned        y;
  336.  
  337.             packdouble(pu, &x, &y);
  338.             _fp_current_write_freg(&x, DOUBLE_E(n));
  339.             _fp_current_write_freg(&y, DOUBLE_F(n));
  340.             break;
  341.         }
  342.     case fp_op_extended:
  343.         {
  344.             extended_type   x;
  345.             unsigned        y, z;
  346.             unsigned        unused = 0;
  347.             unpacked        u;
  348.  
  349.             switch (fp_precision) {    /* Implement extended
  350.                          * rounding precision mode. */
  351.             case fp_single:
  352.                 {
  353.                     single_type     tx;
  354.  
  355.                     packsingle(pu, &tx);
  356.                     pu = &u;
  357.                     unpacksingle(pu, tx);
  358.                     break;
  359.                 }
  360.             case fp_double:
  361.                 {
  362.                     double_type     tx;
  363.                     unsigned        ty;
  364.  
  365.                     packdouble(pu, &tx, &ty);
  366.                     pu = &u;
  367.                     unpackdouble(pu, tx, ty);
  368.                     break;
  369.                 }
  370.             }
  371.             packextended(pu, &x, &y, &z);
  372.             _fp_current_write_freg(&x, EXTENDED_E(n));
  373.             _fp_current_write_freg(&y, EXTENDED_F(n));
  374.             _fp_current_write_freg(&z, EXTENDED_FLOW(n));
  375.             _fp_current_write_freg(&unused, EXTENDED_U(n));
  376.             break;
  377.         }
  378.     }
  379. }
  380.  
  381. void
  382. _fp_pack_word(pu, n)
  383.     unsigned       *pu;    /* unpacked operand */
  384.     unsigned        n;    /* register where datum starts */
  385. {
  386.     _fp_current_write_freg(pu, n);
  387. }
  388. #endif /* lint */
  389.